home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10047 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  91 lines

  1. Path: news.uiowa.edu!usenet
  2. From: Greg Hergert <gregory-hergert@uiowa.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: Dynamic allocation of 2D Array Question
  5. Date: 5 Mar 1996 20:09:12 GMT
  6. Organization: University of Iowa
  7. Distribution: world
  8. Message-ID: <4hi718$19hk@flood.weeg.uiowa.edu>
  9. NNTP-Posting-Host: mcm14.itc.uiowa.edu
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (Macintosh; I; PPC)
  14. X-URL: news:comp.lang.c++
  15.  
  16. Hello, I have a question about declairing a two dimensional 
  17. array (matrix) dynamically.  I would like to have a matrix 
  18. that can be created dynamically (not statically).
  19.  
  20. My code here is:
  21. <<< code.h file >>
  22.  
  23. class grid
  24. {
  25.     public:
  26.         grid (int size);  // constructor
  27.         ~grid () {} // destructor
  28.     
  29. private:
  30.     int SizeofGrid;
  31.     enum status { occupied, unoccupied };
  32.     enum map  { visited, unvisited };
  33.     
  34.     struct square
  35.     {
  36.         status S;
  37.         map M;
  38.         int tag;
  39.     };
  40.     
  41.     square* rep [SizeofGrid] [SizeofGrid];
  42.     };
  43.  
  44.  
  45. << code.cpp file  // implementation... >>
  46.  
  47. #include "grid.h"
  48.  
  49. //definition of constructor
  50.  
  51. grid::grid (int size)
  52. {
  53.     rep = new [size] [size];  //create the matrix
  54.     SizeofGrid = size;       // set private member size
  55.     }
  56.  
  57. in my client program:::
  58.  
  59. void main ()
  60. {
  61.     int N;
  62.     cout << "enter size of grid" ;
  63.     cin >> N;
  64.     grid testgrid(N);
  65. }
  66.  
  67.  
  68. The way that I am solving this problem is by declairing my 
  69. matrix as a static array instead of dynamically:
  70. i.e.
  71.  
  72. header file:::
  73. ..    
  74. private:
  75.     square rep [10][10];
  76. ..
  77.  
  78. Is there a way around this, or somewhere where my syntax is
  79. incorrect?  I've tried the code above on Codewarrior, g++
  80. and BC++ 4.5, they all give errors about the declaration
  81. of the array as a 'new'....
  82.  
  83. Thanks for your help!  In addition to posting suggestions
  84. to the newsgroup, could you also e-mail me a copy! :)
  85.  
  86. gregory-hergert@uiowa.edu
  87.  
  88.  
  89.  
  90.  
  91.